package com.sgb.kjwl.viewmodel.self.packers;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.SelfTeamGroupEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfTeamGroupActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfTeamGroupModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private SelfTeamGroupActivity activity;
    private int page = 1;
    private final int limit = 10;

    public SelfTeamGroupModel(SelfTeamGroupActivity activity, SelfRepertory repertory) {
        this.activity = activity;
    }

    /**
     * 请求班组长列表数据
     * @param isLoadMore
     */
    public void requestTeamGroupList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("page", page);
        map.get().put("limit", limit);
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if ("".equals(companyNo) || "0".equals(companyNo)) {
            map.get().put("companyNo", "");
            map.get().put("userNo", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
        } else {
            map.get().put("companyNo", companyNo);
            map.get().put("userNo", "");
        }
        map.get().put("workTypeCode", "");
        map.get().put("leaderName", "");
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().pageClassLeaderList(map.get()).enqueue(new Callback<BaseEntity<List<SelfTeamGroupEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<SelfTeamGroupEntity>>> call, Response<BaseEntity<List<SelfTeamGroupEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() >= limit) {
                        activity.showList(response.body().getData(), isLoadMore, false);
                    } else {
                        activity.showList(response.body().getData(), isLoadMore, true);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<SelfTeamGroupEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                if (!isLoadMore) {
                    activity.showEmpty();
                }
            }
        });

    }

    /*
     * 删除班组长数据
     */
    public void deleteTeamGroup(SelfTeamGroupEntity entity) {
        map.get().clear();
        map.get().put("no", entity.getNo());
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().deleteClassLeader(map.get()).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    MToast.showToast(activity, "删除成功");
                    activity.deleteUpdateView(entity);
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                MToast.showToast(activity, "删除失败！");
            }
        });
    }

    /**
     * 查看详情
     * @param no
     */
    public void getH5Url(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("leaderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + no + "&isdele=1";
                    WebViewX5HostActivity.start(activity, url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
